﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.IO;
using System.Text;

namespace jnlamp.Common
{
    public sealed class SMSSender
    {
        private static string url;
        private static string uname;
        private static string pwd;

        static SMSSender()
        {
            url = ConfigHelper.GetConfigString("url");
            uname = ConfigHelper.GetConfigString("uname");
            pwd = ConfigHelper.GetConfigString("pwd");
        }

        public static string GetHtmlFromUrl(string phs,string text)
        {
            string strRet = null;            
            if (phs == null || phs.Trim().ToString() == "")
            {
                return strRet;
            }

            string targeturl = string.Format(url.Trim().ToString() + "/?Uid={0}&Key={1}&smsMob={2}&smsText={3}", System.Web.HttpUtility.UrlEncode(uname), System.Web.HttpUtility.UrlEncode(pwd), System.Web.HttpUtility.UrlEncode(phs), System.Web.HttpUtility.UrlEncode(text));
            try
            {
                HttpWebRequest hr = (HttpWebRequest)WebRequest.Create(targeturl);
                hr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
                hr.Method = "GET";
                hr.Timeout = 30 * 60 * 1000;
                WebResponse hs = hr.GetResponse();
                Stream sr = hs.GetResponseStream();
                StreamReader ser = new StreamReader(sr, Encoding.Default);
                strRet = ser.ReadToEnd(); 
            }
            catch (Exception ex)
            {
                strRet = null;
            }
            return strRet;
        }
    }
}
